/********************************************************************
Name: Figure A4: Electing a Democratic or Republican Sheriff Does 
	Not Meaningfully Effect ICE Arrest Rates, 2017-2018
Author: Dan Thompson
Date: August 2019
Notes:
********************************************************************/

* Set the working directory 
gl root = "$sanctuary_root"
gl root = "YOUR_WORKING_DIRECTORY"

* Bring in the Trump-era RD analysis data
use "$root/data/trump era rd analysis data.dta", clear
keep arrests_per1k1 arrests_per1k0 treat rv rv2 rv3 treat_rv treat_rv2 treat_rv3 election_id

* Estimate the regression of the pre-post change in arrests on a polynomial
* of the running variable 
gen diff_arrests_per1k = arrests_per1k1 - arrests_per1k0
reg diff_arrests_per1k treat rv rv2 rv3 treat_rv treat_rv2 treat_rv3 ///
	if abs(rv)<25, vce(clust election_id)
predict y_hat if abs(rv)<25 & diff_arrests_per1k!=.
predict pred_error if abs(rv)<25 & diff_arrests_per1k!=., stdp
keep diff_arrests_per1k y_hat rv treat pred_error
keep if abs(rv)<25 & diff_arrests_per1k!=.

* Construct the standard errors
gen pred_err_lower = y_hat - 1.96*pred_error
gen pred_err_upper = y_hat + 1.96*pred_error

* Make the bin means for the plot
gsort -treat rv
gen row = _n if treat==1
gsort treat -rv
replace row = _n if treat==0
gen bin = ceil(row/8)
egen mean_diff_arrests_per1k = mean(diff_arrests_per1k), by(bin treat)
egen mean_rv = mean(rv), by(bin treat)

* Recover the vote share from the running variable
gen dem_vs = rv+50
gen mean_dem_vs = mean_rv+50

* Remove the bins in the bin scatter plot that doesn't have 8 points
egen max_bin = max(bin), by(treat)
drop if bin==max_bin

* Run the plot
twoway (scatter diff_arrests_per dem_vs, mc(gs12) msi(tiny)) ///
	(scatter mean_diff_arrest mean_dem_vs, mc(gs7) msi(small)) ///
	(line pred_err_upper dem_vs if !treat, lc(gs10) lp(dash)) ///
	(line pred_err_upper dem_vs if treat, lc(gs10) lp(dash)) ///
	(line pred_err_lower dem_vs if !treat, lc(gs10) lp(dash)) ///
	(line pred_err_lower dem_vs if treat, lc(gs10) lp(dash)) ///
	(line y_hat dem_vs if !treat, lc(dknavy)) ///
	(line y_hat dem_vs if treat, lc(dknavy)), ///
	xline(50, lc(gs5)) ///
	legend(off) ///
	xsc(r(25 75)) xlab(30(10)70, labs(medlarge)) ///
	ysc(r(-1 1.2)) ylab(-1(.5)1.2, labs(medlarge)) ///
	yti("Change in Arrests per 1k Residents", si(medlarge)) ///
	xti("Dem Vote Share (%)", si(medlarge)) ///
	bgcolor(white) graphregion(color(white)) ///
	name("rd_plot_trump_diff", replace)
